package com.wmx.thread.synchronize_and_lock;

import lombok.Getter;

import java.util.concurrent.atomic.AtomicInteger;

/**********************************
 * @author wmx
 * @date 2019/8/21 15:48
 * @description:
 * @version 1.0
 ************************************/
@Getter
public class MyTryLock {
    private final AtomicInteger atomicInteger = new AtomicInteger(0);
    private Thread  localThread;
    public boolean getTryLock(){
        boolean success = atomicInteger.compareAndSet(0,1);
        if(success){ //注意要将获得锁的设置到LocalThread
            localThread = Thread.currentThread();
        }

       return success;
    }


    public void unLock(){
        if(0 == atomicInteger.get()){
            return;
        }
        System.out.println( Thread.currentThread().getName());
        System.out.println( localThread.getName());
        if(localThread == Thread.currentThread()){
            atomicInteger.compareAndSet(1,0);
        }



    };
}
